package com.industrial.governance.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.util.IdUtil;
import com.industrial.governance.domain.CountyCount;
import com.industrial.governance.domain.PreMuImportDetailInfo;
import com.industrial.governance.domain.SysArea;
import com.industrial.governance.mapper.PreMuImportDetailInfoMapper;
import com.industrial.governance.service.IPreMuImportDetailInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 导入数据详情Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-29
 */
@Service
public class PreMuImportDetailInfoServiceImpl implements IPreMuImportDetailInfoService
{
    @Autowired
    private PreMuImportDetailInfoMapper preMuImportDetailInfoMapper;

    /**
     * 查询导入数据详情
     *
     * @param baseId 导入数据详情主键
     * @return 导入数据详情
     */
    @Override
    public PreMuImportDetailInfo selectPreMuImportDetailInfoByBaseId(String baseId)
    {
        return preMuImportDetailInfoMapper.selectPreMuImportDetailInfoByBaseId(baseId);
    }

    /**
     * 查询导入数据详情列表
     *
     * @param preMuImportDetailInfo 导入数据详情
     * @return 导入数据详情
     */
    @Override
    public List<PreMuImportDetailInfo> selectPreMuImportDetailInfoList(PreMuImportDetailInfo preMuImportDetailInfo)
    {
        List<PreMuImportDetailInfo> preMuImportDetailInfos = preMuImportDetailInfoMapper.selectPreMuImportDetailInfoList(preMuImportDetailInfo);
        //查询山东省所有的区县
        List<SysArea> allCountyList = preMuImportDetailInfoMapper.selectShanDongCounty(preMuImportDetailInfo);

        if(allCountyList == null || allCountyList.isEmpty()){
            throw new RuntimeException("未找到行政区数据");
        }

        Map<String, String> areaMap = allCountyList.stream().collect(Collectors.toMap(SysArea::getAreaCode, SysArea::getBaseName,(n,o) -> {
            throw new IllegalStateException(String.format("Duplicate key %s", n));
        }, LinkedHashMap::new));

        Map<String, PreMuImportDetailInfo> listResultMap = preMuImportDetailInfos.stream().collect(Collectors.toMap(PreMuImportDetailInfo::getCounty, v -> v));

        List<PreMuImportDetailInfo> list = new ArrayList<>();

        for (Map.Entry<String, String> temp : areaMap.entrySet()) {
            String key = temp.getKey();
            if(!listResultMap.containsKey(key)){
                PreMuImportDetailInfo r = new PreMuImportDetailInfo();
                r.setYears(preMuImportDetailInfo.getYears());
                r.setCounty(temp.getValue());
                r.setSuccessCount("-");
                r.setFailCount("-");
                r.setStatus("未导入");
                r.setSource("-");
                r.setCjTime(null);
                list.add(r);
            }else{
                PreMuImportDetailInfo preMuImportDetailInfo1 = listResultMap.get(key);
                preMuImportDetailInfo1.setCounty(temp.getValue());
//                preMuImportDetailInfo1.setStatus("已导入");
                list.add(preMuImportDetailInfo1);
            }
        }

        if(StringUtils.isNotBlank(preMuImportDetailInfo.getStatus())){
            list = list.stream().filter(k -> StringUtils.equals(k.getStatus(),preMuImportDetailInfo.getStatus())).collect(Collectors.toList());
        }

        return list;
    }
    /**
     * 新增导入数据详情
     *
     * @param preMuImportDetailInfo 导入数据详情
     * @return 结果
     */
    @Override
    public int insertPreMuImportDetailInfo(PreMuImportDetailInfo preMuImportDetailInfo)
    {
        return preMuImportDetailInfoMapper.insertPreMuImportDetailInfo(preMuImportDetailInfo);
    }

    /**
     * 修改导入数据详情
     *
     * @param preMuImportDetailInfo 导入数据详情
     * @return 结果
     */
    @Override
    public int updatePreMuImportDetailInfo(PreMuImportDetailInfo preMuImportDetailInfo)
    {
        return preMuImportDetailInfoMapper.updatePreMuImportDetailInfo(preMuImportDetailInfo);
    }

    /**
     * 批量删除导入数据详情
     *
     * @param baseIds 需要删除的导入数据详情主键
     * @return 结果
     */
    @Override
    public int deletePreMuImportDetailInfoByBaseIds(String[] baseIds)
    {
        return preMuImportDetailInfoMapper.deletePreMuImportDetailInfoByBaseIds(baseIds);
    }

    /**
     * 删除导入数据详情信息
     *
     * @param baseId 导入数据详情主键
     * @return 结果
     */
    @Override
    public int deletePreMuImportDetailInfoByBaseId(String baseId)
    {
        return preMuImportDetailInfoMapper.deletePreMuImportDetailInfoByBaseId(baseId);
    }

    /**
     * @param params
     * @return
     */
    @Override
    public List<PreMuImportDetailInfo> queryImportStatus(PreMuImportDetailInfo params) {
        return null;
    }

    @Override
    public void selectShuiWuJuData() {
        List<CountyCount> countyCounts = preMuImportDetailInfoMapper.selectShuiWuImportCount();
        for(CountyCount countyCount : countyCounts){
            PreMuImportDetailInfo preMuImportDetailInfo = new PreMuImportDetailInfo();
            preMuImportDetailInfo.setCounty(countyCount.getCounty());
            preMuImportDetailInfo.setSuccessCount(countyCount.getCount());
            preMuImportDetailInfo.setFailCount("0");
            preMuImportDetailInfo.setProvince("370000");
            preMuImportDetailInfo.setCity(countyCount.getParentCode());
            preMuImportDetailInfo.setYears("2023");
            preMuImportDetailInfo.setStatus("全部导入");
            preMuImportDetailInfo.setDpetCode("shuiwuju");
        }

    }
}

